//定义关于counter的store
import { defineStore } from "pinia";
import { Service } from "../requests/index.ts";
import { message } from "ant-design-vue";
import project from "./project.js";

/*defineStore 是需要传参数的，其中第一个参数是id，就是一个唯一的值，
简单点说就可以理解成是一个命名空间.
第二个参数就是一个对象，里面有三个模块需要处理，第一个是 state，
第二个是 getters，第三个是 actions。
*/
const useOTN = defineStore("otn", {
  state: () => ({
    otnList: [], //OTN列表
    currentOTN: {}, //当前OTN名称
    currentNet: "NETONE", //当前网络
  }),

  getters: {
    //基本使用
    otnNameListSelect(state) {
      return state.otnList.map((item) => {
        return { value: item.name, label: item.name };
      });
    },
  },

  actions: {
    //基本使用
    selectCurrentOTN(name) {
      return (this.currentOTN.name = name);
    },
    //获取OTN列表
    async getOTNList(projectName, net = "NETONE") {
      this.currentNet = net;
      const res = await Service.queryLinkUsingGet(projectName, "OTN", net);
      if (res.respCode === 200) {
        console.log("getOTNList", res);
        this.otnList = res.data;
      } else {
        message.error(res.message);
      }
      return res.data;
    },
    //添加单个OTN
    async addSingleOTN(projectName, otn, net = null) {
      const currentNetValue = net || this.currentNet;
      console.log("otn", otn);
      const res = await Service.uploadSingOTNLinkUsingPost(
        projectName,
        otn,
        currentNetValue
      );
      // console.log("res",res)
      if (res.respCode === 200) {
        this.otnList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      // console.log("addSingleOTN",this.otnList)
      return this.otnList;
    },
    //文件上传
    async uploadOTNFile(file, projectName, net = null) {
      const currentNetValue = net || this.currentNet;
      const res = await Service.uploadLinkUsingPost(
        file,
        projectName,
        "OTN",
        currentNetValue
      );
      console.log("uploadOTNFile", res);
      if (res.respCode === 200) {
        this.otnList = res.data;
        message.success(file.name + "上传成功");
        return res.data;
      } else {
        message.error(res.message);
      }
      // console.log("addSingleOTN",this.otnList)
      return this.otnList;
    },
    //删除OTN
    async delSingleOTN(projectName, name, net = null) {
      const currentNetValue = net || this.currentNet;
      const res = await Service.deleteNetUsingDelete(
        projectName,
        name,
        "OTN",
        currentNetValue
      );
      console.log("deleteOTNUsingDelete", res);
      if (res.respCode === 200) {
        this.otnList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      return this.otnList;
    },

    //修改单个OTN
    async updateSingleOTN(projectName, otn, net = null) {
      const currentNetValue = net || this.currentNet;
      const res = await Service.modifyOTNUsingPut(
        projectName,
        otn,
        currentNetValue
      );
      console.log("updateSingleOTN", res);
      if (res.respCode === 200) {
        this.otnList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      return this.otnList;
    },
  },
});

//暴露这个otn模块
export default useOTN;
